package com.richard.base

import android.app.Activity
import android.os.Bundle
import androidx.multidex.MultiDexApplication
import com.richard.base.ui.BaseActivity
import com.richard.base.common.isMainProcess
import com.richard.base.starter.*
import com.wxy.appstartfaster.dispatcher.AppStartTaskDispatcher
import kotlin.properties.Delegates

/**
 ***************************************
 * 项目名称:base
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2019/12/26     11:29 AM
 * 用途 ,基础 application
 ***************************************

 */
abstract class BaseApplication : MultiDexApplication() {
    companion object {
        var instance: BaseApplication by Delegates.notNull()
        var countActivity: Int = 0
        var isActivityInFrontGound: Boolean = true
        var currentActivity: String = ""
        var currentActivityStatisticPageName: String? = null
        var showLog: Boolean = false
    }



    override fun onCreate() {
        super.onCreate()
        instance = this
        showLog = BuildConfig.DEBUG
        var mainProcess = isMainProcess()

        if (mainProcess) {
            initStarter()
            registAllActivityLife()
        }
    }

    private fun initStarter() {
        AppStartTaskDispatcher.getInstance()
            .setContext(this)
            .addAppStartTask(InitArouterTask())
            .addAppStartTask(InitHomeModuleTask())
            .addAppStartTask(InitMainModuleTask())
            .addAppStartTask(InitMineModuleTask())
            .addAppStartTask(InitLoginModuleTask())
            .addAppStartTask(InitFragmentationTask())
            .addAppStartTask(InitNetComponentTask())
            .addAppStartTask(InitFileTask())
            .addAppStartTask(InitSdkToolTask())
            .addAppStartTask(InitFontTask())
            .start()
            .await()
    }



    /**
     * 声明周期回调
     */
    private fun registAllActivityLife() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityPaused(activity: Activity?) {
            }

            override fun onActivityResumed(activity: Activity?) {
                currentActivity = activity!!::class.java.simpleName

                if (activity is BaseActivity) {
                    currentActivityStatisticPageName = currentActivity
                }
            }


            override fun onActivityStarted(activity: Activity?) {
                if (countActivity == 0) {
                    isActivityInFrontGound = true
                    //初始化ssid
                }
                countActivity++

            }

            override fun onActivityDestroyed(activity: Activity?) {
                var simpleName = activity!!::class.java.simpleName

            }

            override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
            }

            override fun onActivityStopped(activity: Activity?) {
                countActivity--
                if (countActivity == 0) {
                    isActivityInFrontGound = false
                }

            }

            override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
            }
        })
    }

    //*****************debug.login********************************
    open fun isForeground(): Boolean {
        return countActivity > 0
    }


}